package com.yantong.utils.payment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yantong.utils.ConfigUtil;
import com.yantong.utils.Md5_Sign;

import java.util.HashMap;
import java.util.Map;


/**
 * 扫码订单查询接口
 * 
 * @author Lori
 * @version 2018年6月05日
 */

public class Scan_OrderQuery {
	public static void main(String args[]) throws Exception {
		String key =  ConfigUtil.getProperty("HJ_key");
		Map<String, String> map = new HashMap<String, String>();
		map.put("p1_MerchantNo",  ConfigUtil.getProperty("HJ_userNo"));/** 商户编号 */
		map.put("p2_OrderNo", "X_OC_D20190816091449338149"); /**商户订单号*/

		String Strmap = CreateLinkStringByGet1.createLinkStringByGet(map);

		// 签名
		String sign = "";
		sign = Md5_Sign.SignByMD5(Strmap, key);
		map.put("hmac", sign);/** 签名数据 */
		System.out.println("发送:" + JSON.toJSONString(map).toString());

		// post请求参数内容
		HttpRequester hr = new HttpRequester();
		HttpRespons HP = hr.sendPost("https://www.joinpay.com/trade/queryOrder.action", map);
		System.out.println("接收返回参数：" + HP.getContent());
		JSONObject json = JSONObject.parseObject(HP.getContent());
		Map<String, Object> dataMap = JSONObject.parseObject(JSON.toJSONString(json));
		System.out.println(dataMap.get("ra_Status"));
		System.out.println("///"+dataMap.toString());
		boolean yesorno = nosign(HP.getContent(), key);
		if (yesorno) {
			// System.out.println("验签成功");
		} else {
			// System.out.println("验签失败");
		}
	}
	
	/**
	 * 订单查询
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> scanOrder(String OrderNo) throws Exception {
		String key =  ConfigUtil.getProperty("HJ_key");
		Map<String, String> map = new HashMap<String, String>();
		map.put("p1_MerchantNo",  ConfigUtil.getProperty("HJ_userNo"));/** 商户编号 */
		map.put("p2_OrderNo", OrderNo); /**商户订单号*/

		String Strmap = CreateLinkStringByGet1.createLinkStringByGet(map);

		// 签名
		String sign = "";
		sign = Md5_Sign.SignByMD5(Strmap, key);
		map.put("hmac", sign);/** 签名数据 */
		System.out.println("发送:" + JSON.toJSONString(map).toString());

		// post请求参数内容
		HttpRequester hr = new HttpRequester();
		HttpRespons HP = hr.sendPost("https://www.joinpay.com/trade/queryOrder.action", map);
		System.out.println("接收返回参数：" + HP.getContent());
		//转json
		JSONObject json = JSONObject.parseObject(HP.getContent());
		//json转map
		Map<String, Object> dataMap = JSONObject.parseObject(JSON.toJSONString(json));
		System.out.println(dataMap.get("ra_Status"));
		boolean yesorno = nosign(HP.getContent(), key);
		if (yesorno) {
			return dataMap;
		} else {
			return null;
		}
	}
	
	

	private static boolean nosign(String hp, String key) throws Exception {
		System.out.println("接收到：" + hp);
		JSONObject myJson = JSONObject.parseObject(hp);
		Map m = myJson;

		// 返回hmac
		String returnHmac = (String) m.remove("hmac");
		System.out.println(m.toString());
		String Strmap = CreateLinkStringByGet1.createLinkStringByGet(m);

		// 返回参数组装hmac
		String hmac1 = Md5_Sign.SignByMD5(Strmap, key);
		return hmac1.equals(returnHmac);

	}
}
